package org.gzc.service.impl;

import org.gzc.dao.NoticeMapper;
import org.gzc.pojo.Notice;
import org.gzc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 添加一个公告
     *
     * @param notice
     * @return
     */
    @Override
    public int addNotice(Notice notice) {
        return noticeMapper.insertSelective(notice);
    }

    /**
     * 更新公告
     *
     * @param notice
     * @return
     */
    @Override
    public int updateNotice(Notice notice) {
        return noticeMapper.updateByPrimaryKeySelective(notice);
    }

    /**
     * 单项或批量删除
     *
     * @param notices
     * @return
     */
    @Override
    public int deleteNotice(List<Notice> notices) {
        int num = 0;
        for (Notice notice : notices) {
            num += noticeMapper.deleteByPrimaryKey(notice.getId());
        }
        return num;
    }

    /**
     * 模糊或精准查询公告，分页显示
     *
     * @param notice
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Notice> queryNoticesByPaging(Notice notice, int page, int limit) {
        Map<String, Object> param = new HashMap<>();
        param.put("notice", notice);
        param.put("page", page);
        param.put("limit", limit);
        return noticeMapper.selectNoticeByPaging(param);
    }

    @Override
    public int getTotal() {
        return noticeMapper.selectTotalNotice();
    }
}
